package com.i8office.android.zhihuixinwu.module.presenter;

import android.text.TextUtils;

import com.cyan.tools.callback.SimpleCallback;
import com.cyan.tools.util.RxBus;
import com.i8office.android.zhihuixinwu.bean.Code;
import com.i8office.android.zhihuixinwu.bean.WorkFlowUnRead;
import com.i8office.android.zhihuixinwu.module.contract.OfficeContract;
import com.i8office.android.zhihuixinwu.module.model.OfficeModel;

import java.util.ArrayList;
import java.util.Map;

import bean.WorkFlow;

/**
 * Created by pc on 2018/7/7.
 */

public class OfficePresenter extends BasePresenterImpl<OfficeContract.View> implements OfficeContract.Presenter {
    private OfficeContract.Model officeModel;

    public OfficePresenter(OfficeContract.View view) {
        super(view);
        officeModel = new OfficeModel();
    }

    @Override
    public void getDeals(Map<String, String> param) {
        addDisposable(officeModel.getDeals(new SimpleCallback<ArrayList<WorkFlow>>(mView) {
            @Override
            public void requestSuccess(ArrayList<WorkFlow> workFlows) {
                if (workFlows.size() >= 0)
                    mView.showResult(workFlows);
//                else mView.showEmpty();
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.showError();
            }
        }, param));
    }

    @Override
    public void getPending(Map<String, String> param) {
        addDisposable(officeModel.getPending(new SimpleCallback<ArrayList<WorkFlow>>(mView) {
            @Override
            public void requestSuccess(ArrayList<WorkFlow> workFlows) {
                mView.showResult(workFlows);
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.showError();
            }
        }, param));
    }

    @Override
    public void getPendingDep(Map<String, String> param) {
        addDisposable(officeModel.getPendingDep(new SimpleCallback<ArrayList<WorkFlow>>(mView) {
            @Override
            public void requestSuccess(ArrayList<WorkFlow> workFlows) {
                mView.showResult(workFlows);
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.showError();
            }
        }, param));
    }
    @Override
    public void getPendingDepDeal(Map<String, String> param) {
        addDisposable(officeModel.getPendingDepDeal(new SimpleCallback<ArrayList<WorkFlow>>(mView) {
            @Override
            public void requestSuccess(ArrayList<WorkFlow> workFlows) {
                mView.showResult(workFlows);
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.showError();
            }
        }, param));
    }

    @Override
    public void getNotify(Map<String, String> param) {
        addDisposable(officeModel.getNotify(new SimpleCallback<WorkFlowUnRead>(mView) {
            @Override
            public void requestSuccess(WorkFlowUnRead workFlowUnRead) {
                if(!workFlowUnRead.Succeed){
                    if(!TextUtils.isEmpty(workFlowUnRead.Message)){
                        mView.toast(workFlowUnRead.Message);
                    }
                    mView.showError();
                    return;
                }
                mView.showResult(workFlowUnRead.Data);
                if (workFlowUnRead.TotalInfo != null)
                    mView.showUnReadNum(workFlowUnRead.TotalInfo.TotalRecouds);
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.showError();
            }
        }, param));
    }

    @Override
    public void read(Map<String, String> params) {
        addDisposable(officeModel.read(new SimpleCallback<Code>(mView) {
            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
            }

            @Override
            public void requestSuccess(Code data) {
                super.requestSuccess(data);
                RxBus.get().post("read","");
            }
        }, params));
    }

    @Override
    public void getFavoriteList(Map<String, String> params) {
        addDisposable(officeModel.getFavoriteList(new SimpleCallback<ArrayList<WorkFlow>>(mView) {
            @Override
            public void requestSuccess(ArrayList<WorkFlow> workFlows) {
                mView.showResult(workFlows);
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.showError();
            }
        }, params));
    }
    @Override
    public void getFavoriteDepList(Map<String, String> params) {
        addDisposable(officeModel.getFavoriteDepList(new SimpleCallback<ArrayList<WorkFlow>>(mView) {
            @Override
            public void requestSuccess(ArrayList<WorkFlow> workFlows) {
                mView.showResult(workFlows);
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.showError();
            }
        }, params));
    }
}
